
var textureLogo : Texture;							// [TEXTURE]	Logo
var textureTrans : GUITexture;						// [GUITEXTURE]	Transition
var audioBeep : AudioSource;						// [AUDIO]		Rocket rotation beep
var audioTransition : AudioSource;					// [AUDIO]		Transition sound
var audioBGM : AudioSource;							// [AUDIO]		BGM

private var g_fButtonWidth : float = 300.0f;
private var g_fButtonHeight : float = 75.0f;
private var g_fLogoWidth : float = 800.0f;
private var g_fLogoHeight : float = 400.0f;

private var g_vecScreenCenter : Vector2 = new Vector2( Screen.width * 0.5f, Screen.height * 0.5f );
private var g_rtWindow : Rect = new Rect( 200.0f, 100.0f, 400.0f, 300.0f );

private var STARTSTATE_MAIN : int = 0;
private var STARTSTATE_RANK : int = 1;
private var STARTSTATE_OPTION : int = 2;
private var STARTSTATE_START : int = 3;
private var g_StartState : int = STARTSTATE_MAIN ;

private var g_bReverseButton : boolean = false;
private var g_fMusicVolume : float = 1.0f;
private var g_fEffectVolume : float = 1.0f;

// Rank
private var g_strURL : String = "http://desti.ruree.net/node/rocket/";
private var g_dataRank : LitJson.JsonData;

private var g_sizeWindow : Vector2 = new Vector2( 1024.0f, 768.0f );

function OnGUI()
{
	if( textureTrans.color.a < 0.5f )
		GUI.DrawTexture( Rect(g_vecScreenCenter.x - g_fLogoWidth * 0.5f, g_vecScreenCenter.y - g_fLogoHeight, g_fLogoWidth, g_fLogoHeight), textureLogo, ScaleMode.ScaleToFit, true );
	
	switch( g_StartState )
	{
	case STARTSTATE_MAIN:
		if( GUI.Button( Rect(g_vecScreenCenter.x - g_fButtonWidth * 0.5f, g_vecScreenCenter.y, g_fButtonWidth, g_fButtonHeight), "Start") )
		{
	        audioTransition.Play();
			g_StartState = STARTSTATE_START;
		}
		if( GUI.Button( Rect(g_vecScreenCenter.x - g_fButtonWidth * 0.5f, g_vecScreenCenter.y + g_fButtonHeight + 25.0f, g_fButtonWidth, g_fButtonHeight), "Rank") )
		{
			// Play beep sound
			audioBeep.Play();
			UpdateRank();
			g_StartState = STARTSTATE_RANK;
		}
		if( GUI.Button( Rect(g_vecScreenCenter.x - g_fButtonWidth * 0.5f, g_vecScreenCenter.y + g_fButtonHeight * 2.0f + 50.0f, g_fButtonWidth, g_fButtonHeight), "Option") )
		{
			// Play beep sound
			audioBeep.Play();
			g_StartState = STARTSTATE_OPTION;
		}
		break;
	case STARTSTATE_RANK:
		GUI.Window(0, Rect(g_vecScreenCenter.x - g_fButtonWidth * 0.5f, g_vecScreenCenter.y, g_fButtonWidth, g_fButtonHeight * 4.5f), DoWindowRank, "Rank", GUI.skin.GetStyle("window"));
		break;
	case STARTSTATE_OPTION:
		GUI.Window(0, Rect(g_vecScreenCenter.x - g_fButtonWidth * 0.5f, g_vecScreenCenter.y, g_fButtonWidth, g_fButtonHeight * 3.5f), DoWindowOption, "Option", GUI.skin.GetStyle("window"));
		break;
	}
}

function Start()
{
	//if( g_sizeWindow.x > Screen.width )
	//{
	//	g_fButtonWidth *= Screen.width / g_sizeWindow.x;
	//	g_fLogoWidth *= Screen.width / g_sizeWindow.x;
	//}
	
	if( g_sizeWindow.y > Screen.height )
	{
		g_fButtonHeight *= Screen.height / g_sizeWindow.y;
		g_fLogoWidth *= Screen.height / g_sizeWindow.y;
	}
}

function Update ()
{
	switch( g_StartState )
	{
	case STARTSTATE_START:
		// Transition animation
		textureTrans.color.a += Time.deltaTime;
		textureTrans.color.r += Time.deltaTime;
		textureTrans.color.g += Time.deltaTime;
		textureTrans.color.b += Time.deltaTime;
		
		audioBGM.volume -= Time.deltaTime;
		
		if( textureTrans.color.a >= 1.0f )
		{
			//ProcMain.IsReverseButton = g_bReverseButton;
			Application.LoadLevel("GameIntro");
		}
		break;
	}
}


function UpdateRank()
{
	var www : WWW = new WWW( g_strURL + "JSON" );
	yield www;
	
	g_dataRank = LitJson.JsonMapper.ToObject( www.text );
}

function InsertListItem( rank, name, score )
{
    GUILayout.BeginHorizontal();
    GUILayout.Label(rank.ToString(), GUILayout.Width(50));
    GUILayout.Label(name.ToString(), GUILayout.Width(100));
    GUILayout.Label(score.ToString(), GUILayout.Width(100));
    GUILayout.EndHorizontal();
}

function DoWindowRank( windowID : int )
{
    GUILayout.BeginVertical();
        GUILayout.BeginVertical(GUI.skin.GetStyle("box"));
        InsertListItem( "Rank", "Name", "Score" );
        	if( g_dataRank != null )
        	{
	        	var fScore : float;
		        for (var i : int = 0; i < g_dataRank.Count; i++)
		        {
		        	float.TryParse(g_dataRank[i]["score"].ToString(), fScore);
		        	InsertListItem( i+1, g_dataRank[i]["name"], String.Format("{0:0.0}km", fScore * 0.1f));
		        }
		        for ( ; i < 10 ; i++)
		        {
		        	InsertListItem( i+1, "", "");
		        }
			}
        GUILayout.EndVertical();
		if( GUILayout.Button("Back") )
		{
			// Play beep sound
			audioBeep.Play();
			g_StartState = STARTSTATE_MAIN;
		}
    GUILayout.EndVertical();
}

function DoWindowOption( windowID : int )
{
    GUILayout.BeginVertical();
        GUILayout.BeginVertical(GUI.skin.GetStyle("box"), GUILayout.Height(200));
	        g_bReverseButton = GUILayout.Toggle(g_bReverseButton, "Reverse Button");
	   		GUILayout.Label("");
//	        GUILayout.BeginVertical(GUI.skin.GetStyle("box"));
//			   		GUILayout.Label("Music Volume");
//			        g_fMusicVolume = GUILayout.HorizontalSlider(g_fMusicVolume, 0.0f, 1.0f);
//			   		GUILayout.Label("Effect Volume");
//			        g_fEffectVolume = GUILayout.HorizontalSlider(g_fEffectVolume, 0.0f, 1.0f);
//	        GUILayout.EndVertical();
        GUILayout.EndVertical();
		if( GUILayout.Button("Back") )
		{
			// Play beep sound
			audioBeep.Play();
			g_StartState = STARTSTATE_MAIN;
		}
    GUILayout.EndVertical();
}